package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.entity.Commissioner;
import com.service.ICommissionerService;
import com.utils.commons.ResponseBean;
import com.utils.enums.ENUM_ROLES;
import com.utils.enums.ENUM_SUCESS;

@Controller
public class AdminController {
	
	@Autowired
	private ICommissionerService commissionerService;
	
	@RequestMapping(value = "/approvalCommisList", produces = "text/html;charset=UTF-8")
	public String approvalCommisList(String search, Integer page, Integer size, ModelMap map) {
		Commissioner c = new Commissioner();
		if(page == null) {
			page = 1;
		}
		c.setUserName(search);
		c.setRole(ENUM_ROLES.COMMISSIONER.value);
		c.setIsApproval(ENUM_SUCESS.NO.value);
		Page<Commissioner> result = commissionerService.findUserByCondition(c, page, size, null);
		map.put("list", result.getContent());
    	map.put("page", page+1);
    	map.put("search", search);
    	map.put("total",result.getTotalPages());
		return "person/table_approval";
	}
	
	@RequestMapping(value = "/approvalCommis", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String approvalCommis(Long userid) {
		try {
			commissionerService.approvalCommis(userid);
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/deleteCommis", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteCommis(Long deleteuserid) {
		try {
			commissionerService.deleteUser(deleteuserid);
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
}
